package com.app.xinfadi.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.volley.Response;
import com.app.xinfadi.R;
import com.app.xinfadi.base.BaseActivity;
import com.app.xinfadi.dao.HomeDao;
import com.app.xinfadi.dao.NewPurchaseDao;
import com.app.xinfadi.dao_bean.MartsiteJsonBean;
import com.app.xinfadi.dao_bean.NewPurchasePriceJsonBean;
import com.app.xinfadi.dao_bean.PlantingSiteJsonBean;
import com.app.xinfadi.dao_bean.ProductBeanJsonBean;
import com.app.xinfadi.dao_bean.bean.CenterPurchasePlanBean;
import com.app.xinfadi.dao_bean.bean.CommodityCategoryBean;
import com.app.xinfadi.dao_bean.bean.refresh_bean.HomeRefreshBean;
import com.app.xinfadi.db.DBHelper;
import com.app.xinfadi.db.ProductBeanDbHelper;
import com.app.xinfadi.green_dao.dao.ProductBeanDao;
import com.app.xinfadi.green_dao.model.ProductBean;
import com.app.xinfadi.ui.MainActivity;
import com.app.xinfadi.ui.home.view.CommodityCategoryDropPoplistAdapter;
import com.app.xinfadi.ui.home.view.GridViewImageAdapter;
import com.app.xinfadi.ui.home.view.PlantingSiteDropPoplistAdapter;
import com.app.xinfadi.util.LogUtil;
import com.app.xinfadi.util.SharedPreferencesUtil;
import com.app.xinfadi.widget.CalendarPickerViewPopup;
import com.app.xinfadi.widget.DropDownListPopupWindow;
import com.app.xinfadi.widget.MyGridView;
import com.app.xinfadi.widget.gson.Success;
import com.app.xinfadi.widget.image_browser.ImageBrowserActivity;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Function:发起采购报价
 * <br/>
 * Create on:2016/1/21 10:20
 * <br/>
 * Author:WuTengFei
 */
public class NewPurchasePriceActivity extends BaseActivity {
    private static final String TAG = NewPurchasePriceActivity.class.getSimpleName();
    /*商品类别选择*/
    private RadioButton commodityCategoryRBtn;
    /*商品类别选择*/
    private DropDownListPopupWindow commodityCategoryPopupWindow;
    /*商品类别Adapter*/
    private CommodityCategoryDropPoplistAdapter commodityCategoryDropPoplistAdapter;
    /*产品名录按钮*/
    private RadioButton commodityDirectoryRBtn;
    /*产品名录popup*/
//    private DropDownListPopupWindow commodityDirectoryPopupWindow;
    /*产品名录Adapter*/
//    private CommodityCategoryDropPoplistAdapter commodityDirectoryDropPoplistAdapter;
    private PlantingSiteDropPoplistAdapter producingAreaDropPoplistAdapter;

    /*产地选择*/
    private RadioButton producingAreaRbtn;
    /*采购者*/
    private RadioButton buyerRbtn;
    /*产地选择*/
    private DropDownListPopupWindow producingAreaPopupWindow;
    /*基地选择*/
    private DropDownListPopupWindow homeBasePopupWindow;
    /*采购者*/
    private DropDownListPopupWindow buyerPopupWindow;
    private PlantingSiteDropPoplistAdapter buyerDropPoplistAdapter;

    private PlantingSiteDropPoplistAdapter plantingSiteDropPoplistAdapter;

    private RadioButton homeBaseRbtn;
    /*commodityCategoryRBtn 产品按钮位置*/
    private int[] commodityCategoryRBtnPosition;
    private int[] commodityDirectoryRBtnPosition;
    private int[] commodityDirectoryRBtnPosition1;
    private int[] commodityDirectoryRBtnPosition2;

    private int[] producingAreaRbtnPosition;
    private int[] homeBaseRbtnPostion;
    private MyGridView photoGridView;
    /**
     * 选择的图片
     */
    private ArrayList<String> selectedImageUrls;
    private TextView uploadImgTv;
    private View upload_imgLinLay;
    private ArrayList<File> imageFiles;
    private Button submitBtn;
    /*要求交货时间*/
    private RadioButton deliveryTimeRbtn;
    //有效时间
    private RadioButton expiryDateRbtn;
    private CalendarPickerViewPopup deliveryTimeCalendarPickerViewPopup;
    private CalendarPickerViewPopup expiryDateCalendarPickerViewPopup;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    private CenterPurchasePlanBean centerPurchasePlanBean = new CenterPurchasePlanBean();
    private EditText purchaseCountEt1;
    private EditText standardEditT;
    private EditText epriceEt;
    private EditText spriceEt2;
    private EditText qualityEt3;
    private EditText brandEt;
    private List<ProductBean> product1;
    private List<ProductBean> product2;
    private List<ProductBean> product3;
    private List<ProductBean> product4;
    private RadioButton commodityDirectoryRBtn1;
    private RadioButton commodityDirectoryRBtn2;

    private String categoryParentID1 = "-1";
    private String categoryParentID2 = "-1";
    private String categoryParentID3 = "-1";

    private int currentProductSelectIndex = 0;
    private View buyerView;
    private int[] buyerRbtnRosition;


    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.new_purchase_price_activity_layout);
        initToolbar();
        initView();
        getProductAll();
        initData();
        initBuyerPopupwindow();
        initproducingAreaPopupwindow();
        initHomeBasePopupwindow();
        initPhotographView();
        initClanderPop();
        EventBus.getDefault().register(this);

    }

    private void initData() {
        commodityCategoryRBtnPosition = new int[2];
        commodityDirectoryRBtnPosition = new int[2];
        commodityDirectoryRBtnPosition1 = new int[2];
        commodityDirectoryRBtnPosition2 = new int[2];
        producingAreaRbtnPosition = new int[2];
        homeBaseRbtnPostion = new int[2];
        buyerRbtnRosition = new int[2];
    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        TextView title = (TextView) findViewById(R.id.titleTv);
        title.setVisibility(View.VISIBLE);
        title.setText("产地发起报价");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initView() {
        commodityCategoryRBtn = (RadioButton) findViewById(R.id.commodityCategoryRBtn);
        commodityCategoryRBtn.setOnClickListener(this);
        commodityDirectoryRBtn = (RadioButton) findViewById(R.id.commodityDirectoryRBtn);
        commodityDirectoryRBtn.setOnClickListener(this);
        uploadImgTv = (TextView) findViewById(R.id.uploadImgTv);
        uploadImgTv.setOnClickListener(this);
        producingAreaRbtn = (RadioButton) findViewById(R.id.producingAreaRbtn);
        producingAreaRbtn.setOnClickListener(this);
        upload_imgLinLay = findViewById(R.id.upload_imgLinLay);
        upload_imgLinLay.setOnClickListener(this);
        submitBtn = (Button) findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);
        homeBaseRbtn = (RadioButton) findViewById(R.id.homeBaseRbtn);
        homeBaseRbtn.setOnClickListener(this);
        deliveryTimeRbtn = (RadioButton) findViewById(R.id.deliveryTimeRbtn);
        deliveryTimeRbtn.setOnClickListener(this);
        expiryDateRbtn = (RadioButton) findViewById(R.id.expiryDateRbtn);
        expiryDateRbtn.setOnClickListener(this);
        purchaseCountEt1 = (EditText) findViewById(R.id.purchaseCountEt1);
        standardEditT = (EditText) findViewById(R.id.standardEditT);
        spriceEt2 = (EditText) findViewById(R.id.spriceEt2);
        epriceEt = (EditText) findViewById(R.id.epriceEt);
        brandEt = (EditText) findViewById(R.id.brandEt);
        qualityEt3 = (EditText) findViewById(R.id.qualityEt3);
        commodityDirectoryRBtn1 = (RadioButton) findViewById(R.id.commodityDirectoryRBtn1);
        commodityDirectoryRBtn2 = (RadioButton) findViewById(R.id.commodityDirectoryRBtn2);
        commodityDirectoryRBtn1.setOnClickListener(this);
        commodityDirectoryRBtn2.setOnClickListener(this);
        buyerRbtn = (RadioButton) findViewById(R.id.buyerRbtn);
        buyerRbtn.setOnClickListener(this);
        buyerView = findViewById(R.id.buyerView);
        if (SharedPreferencesUtil.getInstance().getUserRole().equals("1")) {
            buyerView.setVisibility(View.VISIBLE);
        }

    }

    /**
     * 采购者
     */
    private void initBuyerPopupwindow() {

        buyerPopupWindow = new DropDownListPopupWindow(this);
        buyerDropPoplistAdapter = new PlantingSiteDropPoplistAdapter(context);
        getBuyer(buyerDropPoplistAdapter);
        buyerPopupWindow.setAdapter(buyerDropPoplistAdapter);

        buyerPopupWindow.getDrop_downlist().setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                buyerRbtn.setText(buyerDropPoplistAdapter.getDatas()
                        .get(position).NAME);
                centerPurchasePlanBean.BUYER_ID = buyerDropPoplistAdapter.getDatas()
                        .get(position).ID;
                buyerPopupWindow.dismiss();
            }
        });
        buyerPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                buyerRbtn.setChecked(false);
            }
        });

    }

    /**
     * 基地选择
     */
    private void initHomeBasePopupwindow() {

        homeBasePopupWindow = new DropDownListPopupWindow(this);
        plantingSiteDropPoplistAdapter = new PlantingSiteDropPoplistAdapter(context);
        getHomeBasePopupwindowListGson(plantingSiteDropPoplistAdapter);
        homeBasePopupWindow.setAdapter(plantingSiteDropPoplistAdapter);

        homeBasePopupWindow.getDrop_downlist().setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                homeBaseRbtn.setText(plantingSiteDropPoplistAdapter.getDatas()
                        .get(position).NAME);
                centerPurchasePlanBean.BASE_ID = plantingSiteDropPoplistAdapter.getDatas()
                        .get(position).ID;
                homeBasePopupWindow.dismiss();
            }
        });
        homeBasePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                homeBaseRbtn.setChecked(false);
            }
        });

    }

    /**
     * 市场选择
     */
    private void initproducingAreaPopupwindow() {

        producingAreaPopupWindow = new DropDownListPopupWindow(this);
        producingAreaDropPoplistAdapter = new PlantingSiteDropPoplistAdapter(context);
        producingAreaPopupWindow.setAdapter(producingAreaDropPoplistAdapter);

        producingAreaPopupWindow.getDrop_downlist().setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                producingAreaRbtn.setText(producingAreaDropPoplistAdapter.getDatas()
                        .get(position).NAME);
                centerPurchasePlanBean.SITE_ID = producingAreaDropPoplistAdapter.getDatas()
                        .get(position).ID;
                producingAreaPopupWindow.dismiss();
            }
        });
        producingAreaPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                producingAreaRbtn.setChecked(false);
            }
        });
        getProducingAreaPopupwindowListGson(producingAreaDropPoplistAdapter);

    }

    private void initCommodityCategoryPopupwindow(String parent_id) {
        if (commodityCategoryPopupWindow == null) {
            commodityCategoryPopupWindow = new DropDownListPopupWindow(this);
            commodityCategoryDropPoplistAdapter = new CommodityCategoryDropPoplistAdapter(context);

            commodityCategoryPopupWindow.getDrop_downlist().setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (currentProductSelectIndex == 0) {
                        commodityDirectoryRBtn.setText("");
                        commodityDirectoryRBtn1.setText("");
                        commodityDirectoryRBtn2.setText("");
                        categoryParentID1 = "-1";
                        categoryParentID2 = "-1";
                        categoryParentID3 = "-1";
                        centerPurchasePlanBean.PRODUCTS_ID = "";
                        categoryParentID1 = commodityCategoryDropPoplistAdapter.getDatas()
                                .get(position).getID();
                        commodityCategoryRBtn.setText(commodityCategoryDropPoplistAdapter.getDatas()
                                .get(position).getNAME());
                    } else if (currentProductSelectIndex == 1) {
                        centerPurchasePlanBean.PRODUCTS_ID = "";

                        commodityDirectoryRBtn1.setText("");
                        commodityDirectoryRBtn2.setText("");
                        categoryParentID2 = commodityCategoryDropPoplistAdapter.getDatas()
                                .get(position).getID();
                        commodityDirectoryRBtn.setText(commodityCategoryDropPoplistAdapter.getDatas()
                                .get(position).getNAME());
                    } else if (currentProductSelectIndex == 2) {
                        centerPurchasePlanBean.PRODUCTS_ID = "";

                        commodityDirectoryRBtn2.setText("");
                        categoryParentID3 = commodityCategoryDropPoplistAdapter.getDatas()
                                .get(position).getID();
                        commodityDirectoryRBtn1.setText(commodityCategoryDropPoplistAdapter.getDatas()
                                .get(position).getNAME());
                    } else if (currentProductSelectIndex == 3) {
                        centerPurchasePlanBean.PRODUCTS_ID = "";
                        commodityDirectoryRBtn2.setText(commodityCategoryDropPoplistAdapter.getDatas()
                                .get(position).getNAME());
                        centerPurchasePlanBean.PRODUCTS_ID = commodityCategoryDropPoplistAdapter.getDatas()
                                .get(position).getID();
                    }

                    commodityCategoryPopupWindow.dismiss();
                }
            });
            commodityCategoryPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {
                    commodityCategoryRBtn.setChecked(false);
                    commodityDirectoryRBtn.setChecked(false);
                    commodityDirectoryRBtn1.setChecked(false);
                    commodityDirectoryRBtn2.setChecked(false);
                }
            });
            commodityCategoryPopupWindow.setAdapter(commodityCategoryDropPoplistAdapter);

        }
        getProductRefreshData(commodityCategoryDropPoplistAdapter, parent_id);


    }

    private void initClanderPop() {
        // calendarPickerViewPopup = new CalendarPickerViewPopup(this);
        expiryDateCalendarPickerViewPopup = new CalendarPickerViewPopup(CalendarPickerViewPopup.Type.SingleSelect, context);
        expiryDateCalendarPickerViewPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                screenLight();
            }
        });

        expiryDateCalendarPickerViewPopup.setOnMyDateSelectedListener(new CalendarPickerViewPopup.OnMyDateSelectedListener() {

            @Override
            public void onDateSelected(Date date) {
                centerPurchasePlanBean.EXPIRY_DATE = sdf.format(date);
                expiryDateRbtn.setText(sdf.format(date));
            }
        });
        deliveryTimeCalendarPickerViewPopup = new CalendarPickerViewPopup(CalendarPickerViewPopup.Type.SingleSelect, context);
        deliveryTimeCalendarPickerViewPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                screenLight();
            }
        });

        deliveryTimeCalendarPickerViewPopup.setOnMyDateSelectedListener(new CalendarPickerViewPopup.OnMyDateSelectedListener() {

            @Override
            public void onDateSelected(Date date) {
                centerPurchasePlanBean.DELIVERY_TIME = sdf.format(date);
                deliveryTimeRbtn.setText(sdf.format(date));
            }
        });

    }

    private void getProduct(final List<ProductBean> productBean, String parent_id) {
        List<ProductBean> productBeans = ProductBeanDbHelper.getInstance(context).getProductAll(parent_id);
        if (productBeans != null) {
            commodityCategoryDropPoplistAdapter.getDatas().clear();
            commodityCategoryDropPoplistAdapter.getDatas().addAll(productBeans);
            commodityCategoryDropPoplistAdapter.notifyDataSetChanged();
            productBean.addAll(productBeans);
        }
//        HomeDao homeDao = new HomeDao();
//        homeDao.getProduct(parent_id, new Response.Listener<ProductBeanJsonBean>() {
//            @Override
//            public void onResponse(ProductBeanJsonBean productBeanJsonBean) {
//                commodityCategoryDropPoplistAdapter.getDatas().clear();
//                commodityCategoryDropPoplistAdapter.getDatas().addAll(productBeanJsonBean.product);
//                commodityCategoryDropPoplistAdapter.notifyDataSetChanged();
//                productBean.addAll(productBeanJsonBean.product);
//
//            }
//        });
    }

    private void getProductAll() {
        NewPurchaseDao homeDao = new NewPurchaseDao();
        homeDao.getProduct(new Response.Listener<ProductBeanJsonBean>() {
            @Override
            public void onResponse(ProductBeanJsonBean productBeanJsonBean) {
                if (productBeanJsonBean.success && productBeanJsonBean.product != null && productBeanJsonBean.product.size() > 0)
                    DBHelper.getInstance(context).saveProducts(productBeanJsonBean.product);
            }
        });
    }

    private void getProductRefreshData(CommodityCategoryDropPoplistAdapter commodityCategoryDropPoplistAdapter, String parent_id) {
        List<ProductBean> productBeans = new ArrayList<>();
        getProduct(productBeans, parent_id);
        commodityCategoryDropPoplistAdapter.getDatas().clear();
        commodityCategoryDropPoplistAdapter.getDatas().addAll(productBeans);
        commodityCategoryDropPoplistAdapter.notifyDataSetChanged();
    }


    private void getProducingAreaPopupwindowListGson(final PlantingSiteDropPoplistAdapter producingAreaPriceListAdapter) {

        NewPurchaseDao homeDao = new NewPurchaseDao();
        homeDao.getMartsite(new Response.Listener<MartsiteJsonBean>() {
            @Override
            public void onResponse(MartsiteJsonBean martsiteJsonBean) {
                if (martsiteJsonBean.success && martsiteJsonBean.martSite != null && martsiteJsonBean.martSite.size() > 0) {
                    producingAreaPriceListAdapter.getDatas().clear();
                    producingAreaPriceListAdapter.setDatas(martsiteJsonBean.martSite);
                    producingAreaPriceListAdapter.notifyDataSetChanged();
                }
            }

        });
    }

    private void getBuyer(final PlantingSiteDropPoplistAdapter producingAreaPriceListAdapter) {
        NewPurchaseDao homeDao = new NewPurchaseDao();
        homeDao.getUsers(new Response.Listener<PlantingSiteJsonBean>() {
            @Override
            public void onResponse(PlantingSiteJsonBean plantingSiteJsonBean) {
                if (plantingSiteJsonBean.success && plantingSiteJsonBean.plantingSite != null && plantingSiteJsonBean.plantingSite.size() > 0) {
                    producingAreaPriceListAdapter.setDatas(plantingSiteJsonBean.plantingSite);

                }
            }
        });
    }

    private void getHomeBasePopupwindowListGson(final PlantingSiteDropPoplistAdapter producingAreaPriceListAdapter) {
        NewPurchaseDao homeDao = new NewPurchaseDao();
        homeDao.getPlantingsite(new Response.Listener<PlantingSiteJsonBean>() {
            @Override
            public void onResponse(PlantingSiteJsonBean plantingSiteJsonBean) {
                if (plantingSiteJsonBean.success) {
                    if (plantingSiteJsonBean != null && plantingSiteJsonBean.plantingSite != null && plantingSiteJsonBean.plantingSite.size() > 0)
                        producingAreaPriceListAdapter.setDatas(plantingSiteJsonBean.plantingSite);
                }
            }
        });
//        homeDao.getPlantingsite(new TextHttpResponseHandler() {
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                LogUtil.e("getPlantingsite error",responseString+" "+throwable.getMessage());
//            }
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, String responseString) {
//                LogUtil.e("getPlantingsite",responseString);
//                PlantingSiteJsonBean plantingSiteJsonBean = new Gson().fromJson(responseString, PlantingSiteJsonBean.class);
//                if (plantingSiteJsonBean != null) {
//                    producingAreaPriceListAdapter.setDatas(plantingSiteJsonBean.plantingSite);
//                }
//            }
//        });

    }

    /**
     * 初始化 拍照View
     */
    private void initPhotographView() {
        selectedImageUrls = new ArrayList<String>();
        photoGridView = (MyGridView) findViewById(R.id.photoGridView);
        initPhotographGridData();

    }

    /**
     * 获取图片选择URL列表 PhotographUrlList
     */
    public void onEventMainThread(ArrayList<String> imageUrl) {
        LogUtil.e(TAG, "onEventMainThread(ArrayList<String> imageUrl)");
        int length = selectedImageUrls.size();

        if (imageUrl != null && imageUrl.size() > 0) {
            upload_imgLinLay.setVisibility(View.GONE);
            if (length > 0) {
                selectedImageUrls.remove(length - 1);
                selectedImageUrls.addAll(imageUrl);
            } else {
                selectedImageUrls.addAll(imageUrl);
            }
            initPhotographGridData();
        } else {
            upload_imgLinLay.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 初始化 拍照界面的 image gridView 数据
     */
    private void initPhotographGridData() {
        int length1 = selectedImageUrls.size();
        if (length1 > 0) {
            photoGridView.setVisibility(View.VISIBLE);
            // 最后一个 添加图片按钮
            selectedImageUrls.add("add");
            GridViewImageAdapter gridViewImageNoAddAdapter = new GridViewImageAdapter(context, selectedImageUrls);
            photoGridView.setAdapter(gridViewImageNoAddAdapter);
            gridViewImageNoAddAdapter.setAddImageClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intentToImageBrowserActivity(ImageBrowserActivity.IMAGE_EXPORE);
                }
            });
//            savePhotographData(false);
//            } else {
//                GridViewImageNoAddAdapter gridViewImageNoAddAdapter = new GridViewImageNoAddAdapter(context, selectedImageUrls);
//                photoGridView.setAdapter(gridViewImageNoAddAdapter);
//            }

        } else {
            photoGridView.setVisibility(View.GONE);
        }
    }

    /**
     * @param browser_type ImageBrowserActivity.IMAGE_EXPORE 相册
     *                     ImageBrowserActivity.ESCORT_REPORT_EXPORE 陪护报告图片
     */
    private void intentToImageBrowserActivity(int browser_type) {
        Intent intent = new Intent(context, ImageBrowserActivity.class);
        intent.putExtra("selected_size", selectedImageUrls.size() - 1 > 0 ? selectedImageUrls.size() - 1 : 0);
        intent.putExtra("browser_type", browser_type);
        ((BaseActivity) context).startActivityForResult(intent, 10);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.commodityCategoryRBtn:
                currentProductSelectIndex = 0;
                initCommodityCategoryPopupwindow("0");
                commodityCategoryRBtn.getLocationInWindow(commodityCategoryRBtnPosition);
                commodityCategoryPopupWindow.showAsDropDown(commodityCategoryRBtn);
                commodityCategoryPopupWindow.update(commodityCategoryRBtnPosition[0], commodityCategoryRBtnPosition[1]
                                + commodityCategoryRBtn.getHeight(), myApplication.screenWidth - commodityCategoryRBtnPosition[0] - 5,
                        ViewGroup.LayoutParams.WRAP_CONTENT);


                break;
            case R.id.commodityDirectoryRBtn:


                if (!categoryParentID1.equals("-1")) {
                    currentProductSelectIndex = 1;

                    initCommodityCategoryPopupwindow(categoryParentID1 + "");
                    commodityDirectoryRBtn.getLocationInWindow(commodityDirectoryRBtnPosition);
                    commodityCategoryPopupWindow.showAsDropDown(commodityDirectoryRBtn);
                    commodityCategoryPopupWindow.update(commodityDirectoryRBtnPosition[0], commodityDirectoryRBtnPosition[1]
                                    + commodityDirectoryRBtn.getHeight(), myApplication.screenWidth - commodityDirectoryRBtnPosition[0] - 5,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                }
//                commodityDirectoryRBtn.getLocationInWindow(commodityDirectoryRBtnPosition);
//                commodityDirectoryPopupWindow.showAsDropDown(commodityDirectoryRBtn);
//                commodityDirectoryPopupWindow.update(commodityDirectoryRBtnPosition[0], commodityDirectoryRBtnPosition[1]
//                                + commodityDirectoryRBtn.getHeight(), myApplication.screenWidth - commodityDirectoryRBtnPosition[0] - 5,
//                        ViewGroup.LayoutParams.WRAP_CONTENT);
                break;

            case R.id.commodityDirectoryRBtn1:
                if (!categoryParentID2.equals("-1")) {

                    currentProductSelectIndex = 2;

                    initCommodityCategoryPopupwindow(categoryParentID2 + "");
                    commodityDirectoryRBtn1.getLocationInWindow(commodityDirectoryRBtnPosition1);
                    commodityCategoryPopupWindow.showAsDropDown(commodityDirectoryRBtn1);
                    commodityCategoryPopupWindow.update(commodityDirectoryRBtnPosition1[0], commodityDirectoryRBtnPosition1[1]
                                    + commodityDirectoryRBtn1.getHeight(), myApplication.screenWidth - commodityDirectoryRBtnPosition1[0] - 5,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                }
                break;
            case R.id.commodityDirectoryRBtn2:
                if (!categoryParentID3.equals("-1")) {

                    currentProductSelectIndex = 3;

                    initCommodityCategoryPopupwindow(categoryParentID3 + "");
                    commodityDirectoryRBtn2.getLocationInWindow(commodityDirectoryRBtnPosition2);
                    commodityCategoryPopupWindow.showAsDropDown(commodityDirectoryRBtn2);
                    commodityCategoryPopupWindow.update(commodityDirectoryRBtnPosition2[0], commodityDirectoryRBtnPosition2[1]
                                    + commodityDirectoryRBtn2.getHeight(), myApplication.screenWidth - commodityDirectoryRBtnPosition2[0] - 5,
                            ViewGroup.LayoutParams.WRAP_CONTENT);

                }
                break;
            case R.id.uploadImgTv:
                intentToImageBrowserActivity(ImageBrowserActivity.IMAGE_EXPORE);
                break;
            case R.id.upload_imgLinLay:
                intentToImageBrowserActivity(ImageBrowserActivity.IMAGE_EXPORE);

                break;
            case R.id.producingAreaRbtn:
                producingAreaRbtn.getLocationInWindow(producingAreaRbtnPosition);
                producingAreaPopupWindow.showAsDropDown(producingAreaRbtn);
                producingAreaPopupWindow.update(producingAreaRbtnPosition[0], producingAreaRbtnPosition[1]
                                + producingAreaRbtn.getHeight(), myApplication.screenWidth - producingAreaRbtnPosition[0] - 5,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                break;
            case R.id.homeBaseRbtn:
                homeBaseRbtn.getLocationInWindow(homeBaseRbtnPostion);
                homeBasePopupWindow.showAsDropDown(homeBaseRbtn);
                homeBasePopupWindow.update(homeBaseRbtnPostion[0], homeBaseRbtnPostion[1]
                                + homeBaseRbtn.getHeight(), myApplication.screenWidth - homeBaseRbtnPostion[0] - 5,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                break;
            case R.id.buyerRbtn:
                buyerRbtn.getLocationInWindow(buyerRbtnRosition);
                buyerPopupWindow.showAsDropDown(buyerRbtn);
                buyerPopupWindow.update(buyerRbtnRosition[0], buyerRbtnRosition[1]
                                + buyerRbtn.getHeight(), myApplication.screenWidth - buyerRbtnRosition[0] - 5,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                break;
            case R.id.submitBtn:
                centerPurchasePlanBean.PURCHASE_COUNT = purchaseCountEt1.getText().toString().trim();
                centerPurchasePlanBean.STANDARD = standardEditT.getText().toString().trim();
                centerPurchasePlanBean.SPRICE = spriceEt2.getText().toString().trim();
                centerPurchasePlanBean.EPRICE = epriceEt.getText().toString().trim();
                centerPurchasePlanBean.QUALITY = qualityEt3.getText().toString().trim();
                centerPurchasePlanBean.BRAND = brandEt.getText().toString().trim();


                submitNewPurchasePrice();
                break;
            case R.id.expiryDateRbtn:
                closeSoftKeyBoard(this);
                expiryDateCalendarPickerViewPopup.showAtLocation(expiryDateRbtn, Gravity.CENTER, 0, 0);
                screenDarker();
                break;
            case R.id.deliveryTimeRbtn:
                closeSoftKeyBoard(this);
                deliveryTimeCalendarPickerViewPopup.showAtLocation(deliveryTimeRbtn, Gravity.CENTER, 0, 0);
                screenDarker();
                break;

        }

    }

    private void submitNewPurchasePrice() {
        showProgressDialog();
        NewPurchaseDao homeDao = new NewPurchaseDao();
        List<String> image = new ArrayList<String>();
        imageFiles = new ArrayList<File>();
        for (String imageStr : selectedImageUrls) {
            String imageUrl = imageStr.replace("file://", "");
            LogUtil.e(TAG, "PHOTOGRAPH_url:" + imageUrl + " directory" + Environment.getExternalStorageDirectory().getPath());
            File imageFile = new File(imageUrl);
            imageFiles.add(imageFile);
            image.add(imageFiles.size() - 1 + "");
        }
        int imageFileSize = imageFiles.size();
        if (imageFileSize > 0)
            imageFiles.remove(imageFileSize - 1);
        NewPurchasePriceJsonBean newPurchasePriceJsonBean = new NewPurchasePriceJsonBean();
        newPurchasePriceJsonBean.centerPurchasePlanBean = centerPurchasePlanBean;
        String json = new Gson().toJson(newPurchasePriceJsonBean);
        File[] imageFiles1 = imageFiles.toArray(new File[imageFiles.size()]);
        try {
            homeDao.newPurchasePrice(json, imageFiles1, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    Snackbar.make(mToolbar, "提交失败!", Snackbar.LENGTH_SHORT).show();
                    dismissProgressDialog();

                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    Success success = new Gson().fromJson(responseString, Success.class);
                    LogUtil.e(TAG, "success: " + responseString);
                    if (success.success) {
                        Snackbar.make(mToolbar, "提交成功!", Snackbar.LENGTH_SHORT).show();
                        dismissProgressDialog();
                        EventBus.getDefault().post(new HomeRefreshBean(true));
                        popAllActivityExceptOne(MainActivity.class);
                        finish();
                    } else {
                        Snackbar.make(mToolbar, "提交失败!", Snackbar.LENGTH_SHORT).show();
                        dismissProgressDialog();
                    }

                }


            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
